//给你 root1 和 root2 这两棵二叉搜索树。请你返回一个列表，其中包含 两棵树 中的所有整数并按 升序 排序。. 
//
// 
//
// 示例 1： 
//
// 
//
// 
//输入：root1 = [2,1,4], root2 = [1,0,3]
//输出：[0,1,1,2,3,4]
// 
//
// 示例 2： 
//
// 
//
// 
//输入：root1 = [1,null,8], root2 = [8,1]
//输出：[1,1,8,8]
// 
//
// 
//
// 提示： 
//
// 
// 每棵树的节点数在 [0, 5000] 范围内 
// -10⁵ <= Node.val <= 10⁵ 
// 
// Related Topics 树 深度优先搜索 二叉搜索树 二叉树 排序 👍 112 👎 0

package leetcode.editor.cn;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;

public class _1305_AllElementsInTwoBinarySearchTrees {

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public static void main(String[] args) {
        Solution solution = new _1305_AllElementsInTwoBinarySearchTrees().new Solution();
        TreeNode root1 = new TreeNode(2, new TreeNode(1), new TreeNode(4));
        TreeNode root2 = new TreeNode(1, new TreeNode(0), new TreeNode(3));
        System.out.println(solution.getAllElements(root1, root2));
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     * int val;
     * TreeNode left;
     * TreeNode right;
     * TreeNode() {}
     * TreeNode(int val) { this.val = val; }
     * TreeNode(int val, TreeNode left, TreeNode right) {
     * this.val = val;
     * this.left = left;
     * this.right = right;
     * }
     * }
     */
    class Solution {
        public List<Integer> getAllElements(TreeNode root1, TreeNode root2) {
            Queue<Integer> q1 = new ArrayDeque<>();
            nodeToNums(q1, root1);
            Queue<Integer> q2 = new ArrayDeque<>();
            nodeToNums(q2, root2);

            System.out.println("************");
            System.out.println(q1);
            System.out.println(q2);
            System.out.println("************");

            List<Integer> res = new ArrayList<>();
            while (!q1.isEmpty() && !q2.isEmpty()) {
                if (q1.peek() < q2.peek()) {
                    res.add(q1.poll());
                } else {
                    res.add(q2.poll());
                }
            }
            while (!q1.isEmpty()) {
                res.add(q1.poll());
            }
            while (!q2.isEmpty()) {
                res.add(q2.poll());
            }

            return res;
        }

        private void nodeToNums(Queue<Integer> queue, TreeNode root) {
            if (root == null) {
                return;
            }
            if (root.left != null) {
                nodeToNums(queue, root.left);
            }
            queue.add(root.val);
            if (root.right != null) {
                nodeToNums(queue, root.right);
            }
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
//q1 != null && q2 != null &&